package com.zdd.ehe.controller;

import cn.hutool.core.text.csv.CsvUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.Post;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.entity.User;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IPostService;
import com.zdd.ehe.service.IUserService;
import com.zdd.ehe.vo.PostVo;
import com.zdd.ehe.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-18
 */
@Api(tags = "帖子模块")
@RestController
@RequestMapping("/post")
public class PostController {
    @Resource
    private IPostService postService;
    @Resource
    private IUserService userService;

    @ApiOperation("根据类型和编码查询帖子接口")
    @PostMapping("/getallpostbytype")
    public Result getallpostbytype(@RequestParam("type") String type,@RequestParam("code") String code){
        if(type == null || code == null){
            return Result.error(4001,"参数缺失");
        }
        if(type.equals("remen") || type.equals("tongcheng")){
            List<Post> postList = new ArrayList<>();
            if (code.equals("null")){
                QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("type", type).eq("del","1");
                postList = postService.list(queryWrapper);
                if(postList.size() == 0){
                    return Result.error(4003,"无数据");
                }
            }else{
                //System.out.println("同城");
                QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("type", type).eq("location", code).eq("del","1");
                //System.out.println("code: " + code);
                postList = postService.list(queryWrapper);
                if(postList.size() == 0){
                    return Result.error(4003,"无数据");
                }
            }
            List<PostVo> postVoList = new ArrayList<>();
            for(Post post:postList){
                PostVo postVo = new PostVo();
                BeanUtils.copyProperties(post,postVo);
                User user = userService.getById(post.getUserId());
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user,userVo);
                postVo.setUser(userVo);
                postVoList.add(postVo);
            }

            return Result.success("查询成功",postVoList);
        }else{
            return Result.error(4002,"类型错误");
        }
    }


    @ApiOperation("发布帖子接口")
    @PostMapping("/publish")
    public Result publish(@RequestBody Post post){
        if(post.getContent() == null){
            return Result.error(4001,"帖子内容为空");
        }
        User userx = userService.getById(post.getUserId());
        if(userx == null){
            return Result.error(4002,"用户账号不存在");
        }
        post.setPublishTime(LocalDateTime.now());
        post.setLike(0);
        post.setHot(0);
        post.setDel("1");

        int result = postService.insert(post);

        if(result != 1){
            return Result.error(4003,"数据保存失败");
        }

        return Result.success("发布成功");
    }


    @ApiOperation("根据标题获取所有匹配帖子列表接口")
    @GetMapping("/getpostbytitle")
    public Result getpostbytitle(@RequestParam("title") String title){
        if(title == null){
            return Result.error(4001,"缺失参数");
        }

        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        List<Post> postList = postService.list(queryWrapper);
        if(postList.size() == 0){
            return Result.error(4002,"无数据");
        }
        List<PostVo> postVoList = new ArrayList<>();
        for(Post post:postList){
            PostVo postVo = new PostVo();
            BeanUtils.copyProperties(post,postVo);
            User user = userService.getById(post.getUserId());
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user,userVo);
            postVo.setUser(userVo);
            postVoList.add(postVo);
        }
        return Result.success("查询成功",postVoList);
    }

    @ApiOperation("根据ID获取匹配的帖子接口")
    @GetMapping("/getpostbyid")
    public Result getpostbyid(@RequestParam("id") Integer id){
        if(id == null){
            return Result.error(4001,"参数缺失");
        }
        Post post = postService.getById(id);
        if(post == null){
            return Result.error(4002,"帖子编号错误");
        }
        if(post.getDel().equals("0")){
            return Result.error(4003,"该帖子已被封禁");
        }

        PostVo postVo = new PostVo();
        BeanUtils.copyProperties(post,postVo);
        User user = userService.getById(post.getUserId());
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        postVo.setUser(userVo);
        return Result.success("查询成功",postVo);
    }

    @ApiOperation("分页获取帖子接口")
    @PostMapping("/getallpostbypage")
    public Result getAllPostByPage(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        Page<Post> pagex = new Page<>(page,pageSize);
        Page<Post> pageResult = postService.getAllPostByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }


}
